//--------------------------------------------------------------
//
// Qizhi Zhang 
// ezhung@gmail.com
// http://www.moorwind.com
//
//--------------------------------------------------------------
package reader.view
{
	import __AS3__.vec.Vector;
	
	import flash.display.StageDisplayState;
	import flash.events.Event;
	
	import mx.managers.PopUpManager;
	
	import org.puremvc.as3.multicore.interfaces.IMediator;
	import org.puremvc.as3.multicore.interfaces.INotification;
	
	import reader.ApplicationFacade;
	import reader.core.IGCAble;
	import reader.core.ReaderModelLocator;
	import reader.core.ReaderResource;
	import reader.core.UserConfirmCommandConfig;
	import reader.events.ServiceEvent;
	import reader.events.WriterViewEvent;
	import reader.model.IDataProxy;
	import reader.model.IServiceProxy;
	import reader.model.vo.BlogFetchVO;
	import reader.model.vo.BlogSourceVO;
	import reader.view.components.AddServicePanel;
	import reader.view.components.AddSubscribePanel;
	import reader.view.components.ControlBanner;
	import reader.view.components.ManageServicePanel;

	public class ControlBannerMediator extends BaseApplicationMediator implements IMediator, IGCAble
	{
		////////////////////////////////////////////////////////////////////////////////
		//public properties
		public static const NAME:String = "ControlBannerMediator";
		
		public function get controlBanner():ControlBanner
		{
			return this.viewComponent as ControlBanner;
		}
		
		////////////////////////////////////////////////////////////////////////////////
		//private variables
		
		private var dataProxy:IDataProxy;
		private var serviceProxy:IServiceProxy;
		private var readerModelLocator:ReaderModelLocator = ReaderModelLocator.instance;
		
		//////////////////////////////////////////////////////////////////////////////
		//constructor
		public function ControlBannerMediator(viewComponent:Object=null)
		{
			super(NAME, viewComponent);
			startUp();
		}
		
		//////////////////////////////////////////////////////////////////////////////
		//methods
		
		public function startUp():void
		{
			dataProxy = facade.retrieveProxy(readerModelLocator.sqliteProxyFactory.dataProxyName) as IDataProxy;
			serviceProxy = facade.retrieveProxy(readerModelLocator.sqliteProxyFactory.serviceProxyName) as IServiceProxy;
			
			controlBanner.addEventListener(ControlBanner.SCREEN_CHANGE, onScreenChange_handler, false, 0, true);
			controlBanner.addEventListener(ControlBanner.LOG_OUT, onLogOUt_handler, false, 0, true);
			controlBanner.addEventListener(ControlBanner.SHOW_SUMMARY, onShowSubscribe_handler, false, 0, true);
			controlBanner.addEventListener(ControlBanner.ADD_SUBSCRIBE, onAddSubscribe_handler, false, 0, true);
			controlBanner.addEventListener(ControlBanner.FETCH_STER, onFetchStarItem_handler, false, 0, true);
			controlBanner.addEventListener(ControlBanner.FETCH_ALL, onFetchAllItem_handler, false, 0, true);
			controlBanner.addEventListener(ControlBanner.REQUEST_UPDATE, onRequestUpdate_handler, false, 0, true);
			controlBanner.addEventListener(ControlBanner.SHOW_ABOUT_ME, onShowAbout_handler, false, 0, true);
			controlBanner.addEventListener(ControlBanner.TRAY_DOCK, onTrayDock_handler, false, 0, true);
			controlBanner.addEventListener(ControlBanner.DEL_ACCOUNT, onDelAccount_handler, false, 0, true);
			controlBanner.addEventListener(ControlBanner.ADD_SERVICE, onAddNewService_handler, false, 0, true);
			controlBanner.addEventListener(ControlBanner.MANAGE_SERVICE, onManageService_handler, false, 0, true);
			controlBanner.addEventListener(WriterViewEvent.OPEN_WRITER_VIEW, onOpenWriter_handler, false, 0, true);	
		}
		
		public function clearReference():void
		{
			dataProxy = null;
			controlBanner.removeEventListener(ControlBanner.SCREEN_CHANGE, onScreenChange_handler);
			controlBanner.removeEventListener(ControlBanner.LOG_OUT, onLogOUt_handler);
			controlBanner.removeEventListener(ControlBanner.SHOW_SUMMARY, onShowSubscribe_handler);
			controlBanner.removeEventListener(ControlBanner.ADD_SUBSCRIBE, onAddSubscribe_handler);
			controlBanner.removeEventListener(ControlBanner.FETCH_STER, onFetchStarItem_handler);
			controlBanner.removeEventListener(ControlBanner.FETCH_ALL, onFetchAllItem_handler);
			controlBanner.removeEventListener(WriterViewEvent.OPEN_WRITER_VIEW, onOpenWriter_handler);
			controlBanner.removeEventListener(ControlBanner.REQUEST_UPDATE, onRequestUpdate_handler);
			controlBanner.removeEventListener(ControlBanner.SHOW_ABOUT_ME, onShowAbout_handler);
			controlBanner.removeEventListener(ControlBanner.TRAY_DOCK, onTrayDock_handler);
			controlBanner.removeEventListener(ControlBanner.DEL_ACCOUNT, onDelAccount_handler);
			controlBanner.removeEventListener(ControlBanner.ADD_SERVICE, onAddNewService_handler);
			controlBanner.removeEventListener(ControlBanner.MANAGE_SERVICE, onManageService_handler);
			facade.removeMediator(NAME);	
		}
		
		override public function listNotificationInterests():Vector.<String>
		{
			return Vector.<String>([ApplicationFacade.SCREEN_CHANGED, ApplicationFacade.LOGOUT
									]);
		}
		
		override public function handleNotification(notification:INotification):void
		{
			switch(notification.getName())
			{
				case ApplicationFacade.SCREEN_CHANGED:
					var _screen:String = notification.getBody() as String;
					if(_screen == StageDisplayState.NORMAL)
					{
						controlBanner.screenModeLinkButton.label = ReaderResource.instance.resource.appFullScreen;
					}
					else if(_screen == StageDisplayState.FULL_SCREEN_INTERACTIVE)
					{
						controlBanner.screenModeLinkButton.label = ReaderResource.instance.resource.appNormalScreen;						
					}
					break;
				case ApplicationFacade.LOGOUT:
					sendNotification(ApplicationFacade.BLOG_SUMMARY_SHOW, ApplicationFacade.LOGOUT);
					clearReference();
					break;							
			} 
		}
		
		//////////////////////////////////////////////////////////////////////////////
		//events
		
		/**
		 * delete given user.
		*/
		private function onDelAccount_handler(e:Event):void
		{
			var uf:UserConfirmCommandConfig = new UserConfirmCommandConfig(ReaderResource.instance.resource.appDelAccount, 
					ReaderResource.instance.resource.appDelAccountTip,
					readerModelLocator.airReader, doDeleteAccount);
				sendNotification(ApplicationFacade.REQUEST_USERCONFIRM, uf);
			
		}
		private function doDeleteAccount():void
		{
			sendNotification(ApplicationFacade.DELETE_ACCOUNT);
		}
		
		/**
		 * manage the service.
		*/
		private function onManageService_handler(e:Event):void
		{
			var manageServiceBox:ManageServicePanel = PopUpManager.createPopUp(readerModelLocator.airReader, ManageServicePanel, true) as ManageServicePanel;
			PopUpManager.centerPopUp(manageServiceBox);
			manageServiceBox.addEventListener(ServiceEvent.UPDATE_SERVICE, onUpdateService_handler);
			manageServiceBox.addEventListener(ServiceEvent.DELETE_SERVICE, onDeleteService_handler);
		}
		private function onUpdateService_handler(e:ServiceEvent):void
		{
			serviceProxy.updateService(e.service);
		}
		private function onDeleteService_handler(e:ServiceEvent):void
		{
			serviceProxy.deleteService(e.service);
		}
		
		/**
		 * add new metaweblog service or other service.
		*/
		private function onAddNewService_handler(e:Event):void
		{
			var newServBox:AddServicePanel = PopUpManager.createPopUp(readerModelLocator.airReader, AddServicePanel, true) as AddServicePanel;
			PopUpManager.centerPopUp(newServBox);
			newServBox.addEventListener(ServiceEvent.NEW_SERVICE, onNewService_handler);
		}
		
		private function onNewService_handler(e:ServiceEvent):void
		{
			var newServBox:AddServicePanel = e.target as AddServicePanel;
			newServBox.removeEventListener(ServiceEvent.NEW_SERVICE, onNewService_handler);
			newServBox.closeWindow();
			
			serviceProxy.addService(e.service);
		}
		
		/**
		 * tell application to show about-me panel.
		*/
		private function onShowAbout_handler(e:Event):void
		{
			sendNotification(ApplicationFacade.SHOW_ABOUT);
		}
		
		/**
		 * tell application to updae.
		*/
		private function onRequestUpdate_handler(e:Event):void
		{
			sendNotification(ApplicationFacade.UPDATE_READER);
		}
		
		/**
		 * tell ScreenCommand to change screen mode.
		*/
		private function onScreenChange_handler(e:Event):void
		{			
			sendNotification(ApplicationFacade.CHANGE_SCREEN, readerModelLocator.airReader);
		}
		
		/**
		 * tell the application to hide it.
		*/
		private function onTrayDock_handler(e:Event):void
		{
			sendNotification(ApplicationFacade.TRAY_DOCK);
		}
		
		/**
		 * logout.
		*/
		private function onLogOUt_handler(e:Event):void
		{			
			sendNotification(ApplicationFacade.LOGOUT, readerModelLocator.airReader);
		}
		
		/**
		 * display blog summary  
		*/
		private function onShowSubscribe_handler(e:Event):void
		{
			sendNotification(ApplicationFacade.BLOG_SUMMARY_SHOW);
		}
		
		/**
		 * start add a new sunscribe. 
		*/
		private function onAddSubscribe_handler(e:Event):void
		{
			var panel:AddSubscribePanel = AddSubscribePanel(PopUpManager.createPopUp(ReaderModelLocator.instance.airReader, AddSubscribePanel, true));
			PopUpManager.centerPopUp(panel);
			facade.registerMediator(new AddSubscribeMediator(panel));
		}
		
		/**
		 * fetch blog items that mark as star. 
		*/
		private function onFetchStarItem_handler(e:Event):void
		{
			var bs:BlogSourceVO = new BlogSourceVO();
			bs.blogSourceId = -1;
			bs.blogName = ReaderResource.instance.resource.appStarButton;
			var bf:BlogFetchVO = new BlogFetchVO(bs, null);
			sendNotification(ApplicationFacade.BLOG_FETCH, bf);
			dataProxy.getStarItems();
		}
		
		/**
		 * fetch all blog items that mark as unread. 
		*/
		private function onFetchAllItem_handler(e:Event):void
		{
			var bs:BlogSourceVO = new BlogSourceVO();
			bs.blogSourceId = -1;
			bs.blogName = ReaderResource.instance.resource.appAllButton;
			var bf:BlogFetchVO = new BlogFetchVO(bs, null);
			sendNotification(ApplicationFacade.BLOG_FETCH, bf);
			dataProxy.getAllItems();
		}
		
		/**
		 * open blog writer window. 
		*/
		private function onOpenWriter_handler(e:WriterViewEvent):void
		{
			sendNotification(ApplicationFacade.WRITER_OPEN, null, e.viewType);
		}
		
	}
}